package Controller;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

import Custom.Encrypt;
import Model.User;
import Model.Users;
import View.PanelRegistrationForm;
import View.PopUpFrame;

public class AddUser implements ActionListener {

	private PanelRegistrationForm form;
//	private int abonnement;
	private String name;
	private String insertion;
	private String surename;
	private String emailAdres;
	private String phonenumber;
	private String adres;
	private String zipcode;
	private String city;
	private String password;
	private String confirmPassword;
	private String timestamp = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
	private Encrypt encrypt = new Encrypt();
	private Color textboxColor = new Color(150, 0, 0, 50);

	public AddUser(PanelRegistrationForm form) {
		this.form = form;
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("confirm")) {
			try {
				name = form.getNameField().getText();
				if (name.equals("")) {
					JOptionPane.showMessageDialog(null,
							"U heeft geen voornaam ingegeven dit is verplicht",
							null, JOptionPane.ERROR_MESSAGE);
//					form.getNameField().setBackground(textboxColor);
				}
				if (!(name.equals(""))) {
					insertion = form.getInsertionField().getText();
					surename = form.getSurnameField().getText();
					if(surename.equals("")) {
						JOptionPane
						.showMessageDialog(null,
								"U heeft geen achternaam ingegeven dit is verplicht",
								null, JOptionPane.ERROR_MESSAGE);
//						form.getSurnameField().setBackground(textboxColor);
					}
					if(!(surename.equals(""))){
						emailAdres = form.getEmailField().getText();
						if(emailAdres.equals("")){
							JOptionPane
							.showMessageDialog(null,
									"U heeft geen e-mailadress ingegeven dit is verplicht",
									null, JOptionPane.ERROR_MESSAGE);
//							form.getEmailField().setBackground(textboxColor);
						}
						if(!(emailAdres.equals(""))){
							phonenumber = form.getPhoneField().getText();
							adres = form.getAdresField().getText();
							zipcode = form.getZipcodeField().getText();
							city = form.getCityField().getText();
							password = form.getPasswordField().getText();
							if(password.equals(0)){
								JOptionPane
								.showMessageDialog(null,
										"U heeft geen wachtwoord ingegeven dit is verplicht",
										null, JOptionPane.ERROR_MESSAGE);
//								form.getPasswordField().setBackground(textboxColor);
								form.getPasswordField().setText("");
//								form.getConfirmPassField().setBackground(textboxColor);
								form.getConfirmPassField().setText("");
							}
							if(!password.equals(0)){
								confirmPassword = form.getConfirmPassField().getText();
							}
							if(!password.equals(confirmPassword)){
								JOptionPane
								.showMessageDialog(null,
										"De opgegeven wachtwoorden komen niet overeen",
										null, JOptionPane.ERROR_MESSAGE);
								form.getPasswordField().setText("");
								form.getConfirmPassField().setText("");
							}
							if(password.equals(confirmPassword)){
								password = encrypt.crypt(password);
									
								User g = new User(timestamp, name, insertion, surename, password, emailAdres,
																phonenumber, zipcode, adres,city, 0);
								System.out.println(password);
								Users.getInstance().addUser(g);
								
							int n =	JOptionPane.showConfirmDialog(null, "Zijn uw gegevens correct ingevuld?", 
									null, JOptionPane.YES_NO_OPTION);
							if(n == JOptionPane.YES_OPTION){
								JOptionPane
								.showMessageDialog(null,
										"Gebruiker succesvol geregistreerd",
										null, JOptionPane.PLAIN_MESSAGE, null);
								
								PanelRegistrationForm.selfDestroy();
								PopUpFrame.getInstance().dispose();
								PopUpFrame.selfDestroy();
							
							}else{
								form.getPasswordField().setText("");
								form.getConfirmPassField().setText("");
							}
							}
						}	
					}
				}

			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
}
